<template>
  <div>
    <el-form :inline="true" :model="par" class="demo-form-inline">
      <el-form-item label="用户名称">
        <el-input v-model="par.NickName" placeholder="请输入用户名称" clearable style="width: 200px;" />
      </el-form-item>
      <el-form-item label="审批状态">
        <el-select v-model="par.AppStatus" placeholder="请选择" style="width: 200px;" clearable>
          <el-option label="待审批" :value="1" />
          <el-option label="已通过" :value="2" />
          <el-option label="未通过" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-col :span="11">
          <el-date-picker v-model="par.StartTime" type="date" placeholder="开始时间" style="width: 200px" />
        </el-col>
        <el-col :span="2">
          <span>-</span>
        </el-col>
        <el-col :span="11">
          <el-date-picker v-model="par.EndTime" type="date" placeholder="结束时间" style="width: 200px" />
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="show">搜索</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-table :data="tableData" border style="width: 100% ;height: 71vh;">
      <el-table-column prop="leaveId" label="编号" align="center" width="70" />
      <el-table-column prop="nickName" label="姓名" align="center" width="100" />
      <el-table-column prop="depId" label="部门" align="center" width="100">
        <template v-slot="scoped">
          <span> {{depVule[0].children.find((x: any) => x.value == scoped.row.depId)?.label}} </span>
        </template>
      </el-table-column>
      <el-table-column prop="appStatus" label="审批状态" align="center">
        <template v-slot="scoped">
          {{ApproveStatusData.find((x: any) => x.value == scoped.row.appStatus)?.label}}
        </template>
      </el-table-column>
      <el-table-column prop="leaveType" label="请假别类" align="center" width="100" />
      <el-table-column prop="leavePeriod" label="请假时间" align="center" width="330" />
      <el-table-column prop="leaveTime" label="总工时(H)" align="center" width="100" />
      <el-table-column prop="leaveEndTime" label="报道时间" align="center" width="160">
        <template v-slot="scoped">
          {{ scoped.row.leaveEndTime ? moment(scoped.row.leaveEndTime).format("yyyy-MM-DD HH:mm:ss") : "空" }}
        </template>
      </el-table-column>
      <el-table-column prop="leaveReason" label="请假事由" align="center" width="130" />
      <el-table-column prop="leaveLeader" label="部门领导" align="center" width="200" />
      <el-table-column prop="reason" label="不通过原因" align="center" width="200">
        <template v-slot="scoped">
          {{ scoped.row.reason ? scoped.row.reason : "暂无" }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" width="160">
        <template v-slot="scoped">
          {{ scoped.row.createTime ? moment(scoped.row.createTime).format("yyyy-MM-DD HH:mm:ss") : "空" }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" min-width="155">
        <template v-slot="scoped">
          <el-button link type="warning" size="small" @click="Updates(scoped.row)"
            v-show="store.ccstore.userId == scoped.row.userId && scoped.row.AppStatusss === '0'">修改</el-button>
          <el-button link type="danger" size="small" @click="Deletes(scoped.row.leaveId)"
            v-show="store.ccstore.userId == scoped.row.userId && scoped.row.AppStatusss === '0'">删除</el-button>
          <!-- <el-button link type="primary" size="small" @click="Approves(scoped.row)"
            v-show="store.ccstore.levelRoleId == scoped.row.levelRoleId">审批</el-button> -->
          <el-button link type="primary" size="small" @click="Approves(scoped.row)"
            v-show="store.ccstore.levelRoleId == scoped.row.levelRoleId">审批</el-button>
          <el-button link type="primary" size="small" @click="InLeave(scoped.row)"
            v-show="store.ccstore.levelRoleId != scoped.row.levelRoleId || store.ccstore.userId != route.query.userId">查看详细</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!-- {{ tableData }} -->
  <!-- 分页 -->
  <div class="demo-pagination-block">
    <el-pagination v-model:current-page="page.TotalIndex" v-model:page-size="page.TotalSize"
      :page-sizes="[1, 5, 10, 15, 20, 25, 30]" background="background" layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount" />
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus';
import moment from 'moment';
import { compile, computed, onMounted, reactive, ref, watch } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { useRouter, useRoute } from 'vue-router';
import { useCounterStore } from '@/stores/counter';
import DepData from '@/stores/departmentData'
import axios from 'axios';
import ApproveStatus from '@/stores/AppStatus'

//审批状态
const ApproveStatusData = ApproveStatus;
//部门信息
const depVule = DepData;
//pini保存
const store = useCounterStore();
//跳转
const router = useRouter();
//传参
const route = useRoute();


//#region  审批
const Approves = (data: any) => {
  // console.log(store.ccstore.userId, "审批当期登入人id")
  // console.log(data.userId, "审批当前被审批人id")
  //判断是否当前
  // if (store.ccstore.userId == data.userId) {
  //   ElMessage.error("您不可审批")
  // }
  //跳转到审批页面
  router.push({
    name: "AppLeaveModel",
    query: data
  })
}

//跳转详情页面
const InLeave = (data: any) => {
  //跳转到审批页面
  router.push({
    name: "InLeaveModel",
    query: data
  })
}


//#endregion

//#region 获取记录条数 大于 0 判断 "修改":"删除"消失
const getLeaveRecordCount = async (item: any) => {
  console.log(item.leaveId, "查询的申请id")
  try {
    const res = await axios.get(`https://localhost:7174/api/Approve/GetLeaveRecordCount?LeaveId=${item.leaveId}`)
    // 将返回的结果存储到该条数据的 AppStatus 属性中
    item.AppStatusss = res.data.data
  } catch (error) {
    console.error('获取记录条数失败:', error)
  }
}
//#endregion

//#region 修改跳转

//跳转修改
const Updates = (data: any) => {
  router.push({
    name: "UpdateLeaveModel",
    query: data
  })
}

//删除
const Deletes = (ids: any) => {
  ElMessageBox.confirm(
    '确认删除吗?',
    '提示',
    {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      axios.delete(`https://localhost:7272/api/Approve/DeleteLeaveModel?LeaveId=${ids}`).then(res => {
        if (res.data.code == 204) {
          ElMessage.success(res.data.msg)
          show();
        } else {
          ElMessage.success(res.data.msg)
          show();
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消',
      })
    })
}


//#endregion


//#region 显示

//条件查询
const par = reactive({
  AppStatus: '',
  StartTime: '',
  EndTime: '',
  NickName: '',
})

//分页
const page = reactive({
  TotalIndex: 1,
  TotalSize: 15,
  totalCount: 0,
  totalPage: 0,
})

//存储请假申请表数据集合
const tableData = ref([])
//获取集合数据
const show = () => {
  axios.get("https://localhost:7174/api/Approve/ListLeaveModel", {
    params: {
      LevelRoleId: store.ccstore.levelRoleId,
      UserId: store.ccstore.userId,
      DepId: store.ccstore.depId,
      AppStatus: par.AppStatus,
      StartTime: par.StartTime,
      EndTime: par.EndTime,
      NickName: par.NickName,
      TotalIndex: page.TotalIndex,
      TotalSize: page.TotalSize
    }
  }).then(res => {
    tableData.value = res.data.items
    page.totalCount = res.data.totalCount
    page.totalPage = res.data.totalPage
    console.log(res.data, "显示数据")
  })
}

onMounted(() => {
  show();
})

watch(page, () => { show(); })

//#endregion




</script>

<style scoped>
/* 分页 */
.demo-pagination-block+.demo-pagination-block {
  margin-top: 10px;
}

.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>